﻿{include file="common/header" /}
<script>

    Handlebars.registerHelper("isAssigned",function(value){
        if(value){
            return ' <span style="color: green;">Yes</span>';
		}else {
            return ' <span style="color: red;">No</span>';
		}
	});
    Handlebars.registerHelper("isPositionError",function(data){
        if(data==0){
            return ' <span style="color: red;">Error</span>';
        }else {
            return ' <span style="color: green;">Normal</span>';
        }
    });
    Handlebars.registerHelper("isDeleted",function(data){
        if(data==1){
            return ' <span style="color: red;">Deleted</span>';
        }else {
            return ' <span style="color: green;">No</span>';
        }
    });


</script>

<div class="page-container">
	<form class="text-c" action="{{:url('order/index')}}" method="post" id="myform">
		<select name="businessStatus" id="businessStatus" class="input-text" style="width: 100px;">
			<option value="ALL">ALL</option>
			<option value="noBusiness">NO Business</option>
		</select>
		<input type="text" class="input-text" style="width:250px" placeholder="CustName" value="" name="CustName">
		<input type="hidden" name="WareHouse" id="WareHouse" >
		<button type="button" class="btn btn-success" onclick="getData()"><i class="Hui-iconfont"></i> 搜索</button>
	</form>
	<div class="cl pd-5 bg-1 bk-gray mt-20">
		<span class="l">

			<a href="javascript:;" class="btn btn-primary radius" onclick="reloadOrder()">
				<i class="Hui-iconfont"></i> 重新读取SAP数据
			</a>
		</span>
	</div>
	<p >
		地址错误 <span style="color: red" id="error_position">0</span> 订单数 <span style="color: red" id="order-count">0</span>&nbsp;&nbsp;
		今天不营业订单数 <span style="color: red" id="no_business">0</span>
	</p>
	<style>
		.layui-table{
			display: block !important;
		}
	</style>
	<table id="table"  class="table table-border table-bordered table-bg tablesorter"  >
		<thead>
		<tr class="text-c">
			<th width="50">CustID</th>
			<th width="250" class="sort" > <i class="sort_both" data-filed="No"></i>CustName</th>
			<th width="250" class="sort" > <i class="sort_both" data-filed="Assest"></i> Address</th>
			<th width="90">City</th>
			<th width="90">WareHouse</th>
			<th width="90">GrossWeight</th>
			<th width="90">Day</th>
			<th width="90">Position Error</th>
			<th width="90">Delete</th>
			<th width="90">Opreate</th>
		</tr>
		</thead>
		<tbody id="tbody">

		</tbody>
	</table>
</div>

<div class="m-style M-box"></div>
<script id="table-lists" type="text/template">
	{{#each data}}
	<tr class="text-c">
		<td>{{CustID}}</td>
		<td class="td-status" >{{CustName}}</td>
		<td class="td-status" >{{Address}}</td>
		<td class="td-status">{{City}}</td>
		<td class="td-status">{{WareHouse}}</td>
		<td class="td-status">{{GrossWeight}}</td>
		<td class="td-status">{{Day}}</td>
		<td class="td-status">{{{isPositionError positionStatus}}}</td>
		<td class="td-status">{{{isDeleted isDelete}}}</td>
		<td class="td-status">
			<img src="__STATIC__/static/images/forbid.png" width="18" height="18" title="禁止司机配送" onclick="forbidDriver('{{id}}')">
			<img src="__STATIC__/static/images/delete-order.png" width="18" height="18" title="删除订单" onclick="del('{{id}}')">
			<img src="__STATIC__/static/images/review-order.png" width="18" height="18" title="恢复订单" onclick="review('{{id}}')">

		</td>
	</tr>
	{{/each}}
</script>

<script type="text/javascript">
    $(function () {
        let warehouse = localStorage.getItem('WareHouse');
        if(warehouse == null)
        {
            layer.msg('请选择货仓！')
            return false;
        }
        $('#WareHouse').val(warehouse);
        getData();
    })

    //异步获取数据
    function getData(page = 1){
        $('#WareHouse').val(localStorage.getItem('WareHouse'));
        $.ajax({
            type: 'POST',
            url: '{{:url("/order/index")}}?page='+page,
            data:$('#myform').serialize(),
            dataType: 'json',
            beforeSend:function(){
                var index= layer.msg('加载中...',{time:10000});
                layer.style(index, {
                    zIndex:"19991011"  //防止父框的弹出层显示在子框
                });
            },
            success: function(res){
                closeLayerMsg();
                $('#error_position').text(res.msg.error_position);
                $('#no_business').text(res.msg.no_business);
                var source=$('#table-lists').html();
                var template = Handlebars.compile(source);
                $('#tbody').html(template(res.msg));
                paginate(res.msg.total,res.msg.page);
                $('#order-count').text(res.msg.total);
            },
            error:function(data) {}
        })
    }
    function add(){
        layer_show('添加订单','{{:url('/order/create')}}','800','700');
    }
    //重新加载订单数据
    function reloadOrder()
	{
        parent.loadOrderFromSap()
	}

	function forbidDriver (id) {
        let WareHouse = localStorage.getItem('WareHouse');
        layer_show('禁止司机配送','{{:url('/order/frobidDriver')}}/'+id+'/'+WareHouse,'800','700');
    }

    /**
	 * 删除订单
     * @param id
     */
    function del(id){
        $.ajax({
            type: 'POST',
            url: '{{:url("/order/delete")}}',
            data:{id:id},
            dataType: 'json',
            beforeSend:function(){
                var index= layer.msg('处理中...',{time:10000});
                layer.style(index, {
                    zIndex:"19991011"  //防止父框的弹出层显示在子框
                });
            },
            success: function(res){
                getData();
                closeLayerMsg();
                layer.msg(res.msg,{time:1500});
            },
            error:function(data) {
                layer.msg('异常...',{time:1500});
			}
        })
    }

    /**
	 * 恢复订单
     * @param id
     */
    function review(id){
        $.ajax({
            type: 'POST',
            url: '{{:url("/order/review")}}',
            data:{id:id},
            dataType: 'json',
            beforeSend:function(){
                var index= layer.msg('处理中...',{time:10000});
                layer.style(index, {
                    zIndex:"19991011"  //防止父框的弹出层显示在子框
                });
            },
            success: function(res){
                getData();
                closeLayerMsg();
                layer.msg(res.msg,{time:1500});
            },
            error:function(data) {
                layer.msg('异常...',{time:1500});
            }
        })
    }
</script>



{include file="common/footer" /}